package se19team1e.dao.impl.property;

import java.util.ArrayList;
import java.util.List;

import se19team1e.dao.abstrakt.jaas.JobResponseDAO;
import se19team1e.dao.abstrakt.property.PropertyDAO;
import se19team1e.entity.JobResponseDTO;
import se19team1e.exception.DataAccessException;

/**
 * Concrete class implementation of the Property DAO, Property DAO is handling
 * job response in this implementation.
 * 
 * @author arun
 * 
 */
public class JobResponsePropertyDAO extends PropertyDAO<JobResponseDTO>
		implements JobResponseDAO {
	/**
	 * Constructor that calls the super class PropertyDAO to create Job Advert
	 * Property DAO
	 * 
	 * @param jobResponseProperties
	 */
	public JobResponsePropertyDAO(String jobResponseProperties) {
		super(jobResponseProperties);
	}

	/**
	 * Convert DTO into properties given the Job Advert DTO object
	 * 
	 * @return true if create Job Advert DTO is successful)
	 * @throws DataAccessException
	 */
	@Override
	public boolean create(JobResponseDTO dtoType) throws DataAccessException {

		convertDTOIntoProperties(dtoType);
		persist();
		return true;
	}

	/**
	 * 
	 * Deletes an Job Advert object given its ID
	 * 
	 * @param Id
	 *            of the Job Advert object to be deleted
	 * @return true if delete is successful
	 * @throws DataAccessException
	 * 
	 */
	@Override
	public boolean delete(Object id) throws DataAccessException {

		JobResponseDTO jobResponseDTO = retrieveById(id);
		jobResponseDTO.setDeleted(true);
		convertDTOIntoProperties(jobResponseDTO);
		persist();

		return true;
	}

	/**
	 * DAO will retrieve all Job Advert objects in the persistence storage.
	 * 
	 * @return list of Job Advert DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public List<JobResponseDTO> retrieveAll() throws DataAccessException {
		List<JobResponseDTO> jobResponseDTOList = new ArrayList<JobResponseDTO>();
		int lastSequence = getLastSequence();
		JobResponseDTO jobResponseDTO = null;
		for (int currentSequence = 1; currentSequence <= lastSequence; currentSequence++) {
			jobResponseDTO = new JobResponseDTO();
			populateValuesIntoDTO(String.valueOf(currentSequence),
					jobResponseDTO);
			jobResponseDTOList.add(jobResponseDTO);

		}
		return jobResponseDTOList;
	}

	/**
	 * DAO will retrieve Job Advert objects in the persistence storage based on
	 * the id
	 * 
	 * @param id
	 *            unique id of the object
	 * @return list of Job Advert DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public JobResponseDTO retrieveById(Object id) throws DataAccessException {

		JobResponseDTO jobResponseDTO = new JobResponseDTO();
		populateValuesIntoDTO((String) id, jobResponseDTO);
		return jobResponseDTO;
	}

	/**
	 * DAO will update Job Advert objects in the persistence storage based
	 * 
	 * @param AccountDTO
	 *            Job Advert DataTransfer Object
	 * @return list of Job Advert DataTransfer objects
	 * @throws DataAccessException
	 */
	@Override
	public JobResponseDTO update(JobResponseDTO dto) throws DataAccessException {

		convertDTOIntoProperties(dto);
		persist();
		return retrieveById(dto.getId());
	}

}
